<?php
/**
 * Generates a scramble string to pass to the parser
 *
 * @package Cuber
 * @since   Apr 15, 2007
 * @author  Christophe Thiriot
 */


/**
 * Generates a scramble string to pass to the parser
 *
 * @package Cuber
 */
class Scrambler 
{
    private $moves = array('U','D','R','L','F','B');

    private $dirs = array("", "'", "2");

    private $result_alg = '';

    public function __construct()
    {
        $this->result_alg = $this->gen();
    }

    public function __toString()
    {
        return $this->result_alg;
    }

    /**
     * Return a random generated cube scrambling string
     * 
     * @return string
     */
    public function gen()
    {
        // init the list of bad moves
        $bad = array();
        foreach ($this->moves as $move)
        {
            $bad[$move] = 0;
        }

        $out = "";
        
        foreach (range(1, 25) as $t)
        {
            // Find face to turn.
            do 
            {
              $turn = rand( 0, 5 );
            } while( $bad[$turn] == 1 );

            // add the turn to the returned string
            $out .= $this->moves[$turn] . $this->dirs[rand(0,2)] . ' ';
            // Mark the move as unavailable.
            $bad[$turn] = 1;

            // Mark all adjacent faces as available.
            foreach(range(0, 5) as $i)
            {
                if($i != $turn  &&  ($i^1) != $turn)
                {
                    $bad[$i] = 0;
                }
            }
        }

        return trim($out);
    }
}

?>

